package top.carrotguo.weibo.adapter;

import android.content.Context;
import android.graphics.PointF;
import android.net.Uri;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.ImageViewState;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.sina.weibo.sdk.utils.FileUtils;

import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import top.carrotguo.weibo.R;
import top.carrotguo.weibo.bean.HomeTimeLine;
import top.carrotguo.weibo.utils.FileUtil;
import top.carrotguo.weibo.utils.ImageUtils;

public class ImagePagerAdapter extends PagerAdapter {

    private ArrayList<HomeTimeLine.UsersBean.Pic> mDatas;
    private Context mContext;
    private ArrayList<View> mViews;


    public ImagePagerAdapter(Context context, ArrayList<HomeTimeLine.UsersBean.Pic> datas){
        mContext = context;
        mDatas = datas;
        //初始化view
        mViews = new ArrayList<>();
        for (int i=0; i<mDatas.size(); i++) {
            View view = View.inflate(context,R.layout.item_show_image,null);
            mViews.add(view);
        }
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(mViews.get(position));
        SubsamplingScaleImageView imageView = mViews.get(position).findViewById(R.id.iv_image);
        imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
        imageView.setMinScale(1.0F);//最小显示比例
        imageView.setMaxScale(10.0F);//最大显示比例（太大了图片显示会失真，因为一般微博长图的宽度不会太宽）
        //下面执行计算图片大小以及加载操作
        Glide.with(mContext)
                .load(mDatas.get(position).isHasLarge()?mDatas.get(position).getLarge_pic():mDatas.get(position).getNormal_pic())
                .downloadOnly(new SimpleTarget<File>() {
            @Override
            public void onResourceReady(File resource, Transition<? super File> transition) {
                    // 将保存的图片地址给SubsamplingScaleImageView,这里注意设置ImageViewState设置初始显示比例
                    imageView.setImage(ImageSource.uri(Uri.fromFile(resource)),
                            new ImageViewState(2.5F, new PointF(0, 0), 0));
            }
        });
        return mViews.get(position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(mViews.get(position));
    }

    /**
     * 显示原图
     * @param position
     */
    public void showLargeImage(int position){
        mDatas.get(position).setHasLarge(true);
        SubsamplingScaleImageView imageView = mViews.get(position).findViewById(R.id.iv_image);
        imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
        imageView.setMinScale(1.0F);//最小显示比例
        imageView.setMaxScale(10.0F);//最大显示比例（太大了图片显示会失真，因为一般微博长图的宽度不会太宽）
        //下面执行计算图片大小以及加载操作
        Glide.with(mContext)
                .load(mDatas.get(position).getLarge_pic())
                .downloadOnly(new SimpleTarget<File>() {
                    @Override
                    public void onResourceReady(File resource, Transition<? super File> transition) {
                        // 将保存的图片地址给SubsamplingScaleImageView,这里注意设置ImageViewState设置初始显示比例
                        imageView.setImage(ImageSource.uri(Uri.fromFile(resource)),
                                new ImageViewState(2.5F, new PointF(0, 0), 0));
                    }
                });
    }

}
